#!/bin/bash

# 获取当前脚本的绝对路径
SCRIPT_PATH=$(realpath "$0")

# 获取当前脚本所在的目录
SCRIPT_DIR=$(dirname "${SCRIPT_PATH}")

RC_PATH="${HOME}/.bashrc"

DOTFILES_TOOLS_HOME="${HOME}/.dotfiles-tools"

if [[ -e "${HOME}/.dotfiles-tools" ]]; then
  echo "${HOME}/dotfiles-tools has existed, skip link"
else
  echo "ln -s ${SCRIPT_DIR}/tools ${DOTFILES_TOOLS_HOME}"
  ln -s "${SCRIPT_DIR}/tools" "${HOME}/.dotfiles-tools"
fi

sed -i '/^# << link-dotfiles-tools$/,/^# >> link-dotfiles-tools$/d' "${RC_PATH}"

env_cmd() {
  cat <<EOF

# << link-dotfile-tools
DOTFILES_TOOLS_HOME="${DOTFILES_TOOLS_HOME}"
PATH=\${PATH}:\${DOTFILES_TOOLS_HOME}
# >> link-dotfiles-tools

EOF
}

env_cmd >>${RC_PATH}

echo "add DOTFILES_TOOLS_HOME to ${RC_PATH}, please execute 'source ${RC_PATH}'again."
